import {
  getUserInfo
} from "../../utils/my.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: wx.getStorageSync('userInfo'),
    avatar: '',
    menuInfo: {},
    str: [{
        title: '我的事项',
        url: '/pages/my/matter/matter',
        type: 'page',
        icon: 'my2'
      },
      {
        title: '我的消息',
        url: '/pages/my/message/message',
        type: 'page',
        icon: 'my3'
      },
      {
        title: '隐私政策',
        url: '/pages/my/privacy/privacy',
        type: 'page',
        icon: 'my4'
      },
    ],
    login_identification: null,
    show: false
  },

  getUserInfo() {
    getUserInfo({
      sfz: this.data.userInfo.idNumber
    }).then(res => {
      this.setData({
        avatar: "data:image/gif;base64," + res.data.image
      })
    })
  },
  //跳转页面
  jumpPage(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  jumpMiniProgram() {
    wx.navigateToMiniProgram({
      appId: 'wx6163d215ef686f78',
      path: 'pages/home/index',
      success(res) {
        // 打开成功
      }
    })
  },
  sctt() {
    wx.setStorageSync('redirect', this.route)
    wx.navigateToMiniProgram({
      path: '/pages/humanSociety/appBear',
      appId: 'wx7aa6ad8c15829d96',
      envVersion: 'trial',
      extraData: {
        appId: '202406040001',
        forwardUrl: "https://rs.jshrss.jiangsu.gov.cn/mobile/person/login?appId=202406040001&returnUrl=/pages/login/index&channelType=JSHRSS_WXSP&thirdAppId=wxeee1e9fe5686a91e",
        from: 'miniapp'
      }
    });
  },
  // 退出账号
  logout() {
    const that = this
    wx.showModal({
      title: '提示',
      content: '确定退出吗？',
      success(res) {
        if (res.confirm) {
          wx.clearStorage()
          that.setData({
            userInfo: {},
            avatar: ''
          })
          wx.showToast({
            title: '退出成功',
            icon: 'none'
          })
        }
      }
    })
  },
  showAvatar() {
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        show: true
      })
    } else {
      this.sctt()
    }
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  changeAvatar() {
    const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: 'compressed',
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.cropImage({
          src: tempFilePaths[0],
          cropScale: '1:1',
          success: function (res) {
            that.uploadFile(res.tempFilePath)
          },
          fail: function () {
            that.uploadFile(tempFilePaths[0])
          }
        })
      }
    })
  },
  uploadFile(filePath) {
    const that = this;
    wx.uploadFile({
      url: 'https://njrsxxzx.cn/njrs/imageUpload/uploadFile',
      filePath: filePath,
      name: 'file',
      header:{
        token: wx.getStorageSync('accessToken')
      },
      formData: {
        'sfz': that.data.userInfo.idNumber,
        'name': that.data.userInfo.name,
      },
      success(res) {
        try {
          let data = JSON.parse(res.data)
          if (data.code == 1000) {
            that.setData({
              avatar: "data:image/gif;base64," + data.image,
              show: false
            })
            wx.showToast({
              title: '上传成功',
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: '上传失败',
              icon: 'none'
            })
          }
        } catch (error) {
          wx.showToast({
            title: '上传失败',
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 登录的情况下 去获取用户信息
    if (wx.getStorageSync('userInfo')) {
      this.getUserInfo()
    }
    const top = getApp()
    this.setData({
      menuInfo: top.geibartop.menuInfo,
      // login_identification:util.loginOk,
      userInfo: wx.getStorageSync('userInfo')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})